﻿Public Class LoaiNhaDAO
    Private connect As Connect
    Private sqlstr As String
    Public Sub New()
        connect = New Connect
    End Sub

    Public Function CapNhat(ByVal emp As LoaiNhaDTO, ByVal flag As Boolean)
        Dim maloainha As String = emp.MaLoaiNha
        Dim tenloainha As String = emp.TenLoaiNha

        '-------------------------------------------'
        If flag = True Then
            sqlstr = "INSERT LOAINHA (MaLoaiNha, TenLoaiNha)" & _
                        " VALUES('" & maloainha & "', '" & tenloainha & "')"
        Else
            sqlstr = "UPDATE LOAINHA SET  MaLoaiNha  = '" & maloainha & _
                                        "', TenLoaiNha   = '" & tenloainha & _
                                    "' WHERE MaLoaiNha  = '" & maloainha & "'"

        End If
        Return connect.Execute(sqlstr)
    End Function
    Public Function XoaLoaiNha(ByVal emp As LoaiNhaDTO)
        Dim maloainha As String = emp.MaLoaiNha()
        sqlstr = "DELETE LOAINHA WHERE MaLoaiNha = '" & maloainha & "'"
        Return connect.Execute(sqlstr)
    End Function

    Public Function GetData() As List(Of LoaiNhaDTO)
        Dim i As Integer
        Dim maloainha, tenloainha As String
        Dim lnDTO As LoaiNhaDTO
        Dim lnList As New List(Of LoaiNhaDTO)
        Dim dt As New DataTable
        dt = connect.GetData("select * from LOAINHA")
        For i = 0 To dt.Rows.Count() - 1
            maloainha = dt.Rows(i)("MaLoaiNha").ToString
            tenloainha = dt.Rows(i)("TenLoaiNha").ToString
            lnDTO = New LoaiNhaDTO(maloainha, tenloainha)
            lnList.Add(lnDTO)
        Next
        Return lnList
    End Function
End Class
